package com.heima.admin.service.impl;

import com.heima.admin.feign.WemediaFeign;
import com.heima.admin.service.NewsAuthService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.NewsUpdate;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NewsAuthServiceImpl implements NewsAuthService {

    @Autowired
    private WemediaFeign wemediaFeign;

    /**
     * 查询自媒体文章列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findNews(NewsAuthDto dto) {

        //校验参数
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        return wemediaFeign.findList(dto);
    }

    /**
     * 查询文章详情
     * @param id
     * @return
     */
    @Override
    public ResponseResult findOne(Integer id) {

        //校验参数
        if (null == id) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //响应结果
        WmNewsVo wmNewsVo = wemediaFeign.findWmNewsVo(id);
        return ResponseResult.okResult(wmNewsVo);
    }

    /**
     * 文章审核成功
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateStatus(NewsUpdate dto, boolean type) {

        //校验参数
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //封装参数
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());

        //是否通过审核
        if (type) {
            //审核通过
            wmNews.setStatus((short) 4);
        } else {
            //审核拒绝
            wmNews.setStatus((short) 2);
            wmNews.setReason(dto.getMsg());
        }

        return wemediaFeign.updateWmNews(wmNews);
    }
}
